package while_lang.ast;

import java.util.*;

/**
 * Nodo del AST de While que representa la expresión aritmética de suma.
 * 
 * @author Leonardo Val
 */
public class Addition extends Exp {
	public final Exp operand1, operand2;

	public Addition(Exp operand1, Exp operand2) {
		this.operand1 = operand1;
		this.operand2 = operand2;
	}

	@Override
	public String unparse() {
		return "(" + operand1.unparse() + " + " + operand2.unparse() + ")";
	}

	@Override
	public void halteadNumbers(Map<String, Integer> operands,
			Map<String, Integer> operators) {
		operand1.halteadNumbers(operands, operators);
		operand2.halteadNumbers(operands, operators);
		String key = "+";
		Stmt.incrementarDiccionario(key, operators);
	}

	public static Addition generate(Random random, int min, int max) {
		return new Addition(Exp.generate(random, min - 1, max - 1),
				Exp.generate(random, min - 1, max - 1));
	}

	@Override
	public String toString() {
		return "Addition(" + operand1 + ", " + operand2 + ")";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((operand1 == null) ? 0 : operand1.hashCode());
		result = prime * result
				+ ((operand2 == null) ? 0 : operand2.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Addition other = (Addition) obj;
		if (operand1 == null) {
			if (other.operand1 != null)
				return false;
		} else if (!operand1.equals(other.operand1))
			return false;
		if (operand2 == null) {
			if (other.operand2 != null)
				return false;
		} else if (!operand2.equals(other.operand2))
			return false;
		return true;
	}

}
